package top.infopub.mgr.sys.web;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.generic.GenericController;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.model.sys.ParameterGroupDetail;
import top.infopub.model.sys.Parametergroup;
import top.infopub.tdp.sys.service.ParameterGroupDetailService;
import top.infopub.tdp.sys.service.ParametergroupService;


/**
 * 数据字典参数明细管理
 * @author Awoke
 * @version 2018年10月15日
 * @see ParamGroupDetailController
 * @since
 */
@Controller
@RequestMapping("/paramGroupDetail/{groupNo}")
public class ParamGroupDetailController extends GenericController {

    @Autowired
    private ParameterGroupDetailService parameterGroupDetailService;

    @Autowired
    private ParametergroupService parametergroupService;

    /**
     * 数据字典参数管理
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView paramGroup(@PathVariable String groupNo) {
        Parametergroup parametergroup = parametergroupService.selectByGroupNo(groupNo);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("groupNo", groupNo);
        map.put("bisModify", parametergroup.getBisModify());
        return new ModelAndView("paraMgr/parametergroupDetail-list", map);
    }

    /**
     * 查询数据字典参数明细
     * @return
     */
    @RequestMapping("/queryParamGroupDetail")
    public @ResponseBody RspVo<List<ParameterGroupDetail>> queryParamGroupDetail(@PathVariable String groupNo,
                                                                                      ParameterGroupDetail parameterGroupDetail) {
        return RspVo.getSuccessResponse(parameterGroupDetailService.selectParametergroupByGroupNoSnameSvalue(
            groupNo, parameterGroupDetail));
    }

    /**
     * 新增数据字典参数明细
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/addParamGroupDetail")
    public @ResponseBody RspVo addParamGroupDetail(@PathVariable String groupNo,
                                                        ParameterGroupDetail parameterGroupDetail) {
        Parametergroup parametergroup = parametergroupService.selectByGroupNo(groupNo);
        parameterGroupDetail.setSgroupid(parametergroup.getId());
        // 添加或者修改时，要做 【参数名称】、【参数值】 的唯一性校验，不能重复
        List<ParameterGroupDetail> parameterGroupDetailList = parameterGroupDetailService.selectBySnameOrSvalue(parameterGroupDetail);
        if (parameterGroupDetailList != null && parameterGroupDetailList.size() > 0) {
            return ErrorCodeEnum.ERROR_COMMON_SYSTEM.getRspVo("【参数名称】、【参数值】 ，不能重复！");
        }
        parameterGroupDetailService.insert(parameterGroupDetail);
        return RspVo.getSuccessResponse();
    }

    /**
     * 修改数据字典参数明细
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/updateParamGroupDetail")
    public @ResponseBody RspVo updateParamGroupDetail(@PathVariable String groupNo,
                                                           ParameterGroupDetail parameterGroupDetail) {
        Parametergroup parametergroup = parametergroupService.selectByGroupNo(groupNo);
        parameterGroupDetail.setSgroupid(parametergroup.getId());
        // 添加或者修改时，要做 【参数名称】、【参数值】 的唯一性校验，不能重复
        List<ParameterGroupDetail> parameterGroupDetailList = parameterGroupDetailService.selectBySnameOrSvalue(parameterGroupDetail);
        if (parameterGroupDetailList != null && parameterGroupDetailList.size() > 0) {
            return ErrorCodeEnum.ERROR_COMMON_SYSTEM.getRspVo("【参数名称】、【参数值】 ，不能重复！");
        }
        parameterGroupDetailService.updateByPrimaryKey(parameterGroupDetail);
        return RspVo.getSuccessResponse();
    }

    /**
     * 批量删除数据字典参数明细
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delParamGroupDetail")
    public @ResponseBody RspVo delParamGroupDetail(@PathVariable String groupNo,
                                                        String[] checkboxId) {
        parameterGroupDetailService.batchDeleteByIds(checkboxId);
        return RspVo.getSuccessResponse();
    }

}
